package com.zjdiepu.www.biz.partner;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.diepu.www.client.xhh.partner.request.ApplyLoanRequest;
import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.status.ThirdBusiStatus;
import com.zjdiepu.www.base.enums.status.ThirdCompensatoryStatus;
import com.zjdiepu.www.base.enums.type.ClientType;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.commpent.helper.UserMapSalermanHelper;
import com.zjdiepu.www.commpent.mapper.yw.YwBusinessDetailMapper;
import com.zjdiepu.www.dao.partner.ThYwBusinessMapper;
import com.zjdiepu.www.dao.yw.YwBusinessBuyerMapper;
import com.zjdiepu.www.dao.yw.YwBusinessMapper;
import com.zjdiepu.www.ibiz.partner.IThBusinessRecordService;
import com.zjdiepu.www.ibiz.partner.IThHandleErrService;
import com.zjdiepu.www.ibiz.partner.IThLogsService;
import com.zjdiepu.www.ibiz.partner.IThYwBusinessService;
import com.zjdiepu.www.ibiz.system.IBaseSysParamsService;
import com.zjdiepu.www.model.partner.dto.ThYwBusiness;
import com.zjdiepu.www.model.partner.ext.ThYwBusinessExt;
import com.zjdiepu.www.model.yw.response.YwBusinessDetail;
import com.zjdiepu.www.util.ExceptionHelper;

/**
 * Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年09月21日 10时04分<br/>
 * @since 1.0<br/>
 */
@Service
public class ThYwBusinessServiceImpl extends BaseService implements IThYwBusinessService {
	public static final Logger logger = LoggerFactory.getLogger(ThYwBusinessServiceImpl.class);

	@Autowired
	private ThYwBusinessMapper thYwBusinessMapper;
	@Autowired
	private IThHandleErrService thHandleErrService;
	@Autowired
	private IThLogsService thLogsService;
	@Autowired
	private YwBusinessBuyerMapper ywBusinessBuyerMapper;
	@Autowired
	private YwBusinessMapper ywBusinessMapper;
	@Autowired
	private UserMapSalermanHelper userMapSalermanHelper;
	@Autowired
	private YwBusinessDetailMapper ywBusinessDetailMapper;
	@Autowired
	private IBaseSysParamsService baseSysParamsService;
	@Autowired
	private IThBusinessRecordService thBusinessRecordService;
	
	
	//处理成功记录
	@Override
	public ThYwBusiness saveOrUpdateThYwBusiness(String busiType, String busiId, YwBusinessDetail ywBusinessDetail, ThirdBusiStatus thirdBusiStatus, ThirdCompensatoryStatus thirdCompensatoryStatus,
			ClientType clientType, ApplyLoanRequest applyLoanRequest, String xeApplyNo, String salermanProvince, String currentUserId, String currentUserName){
		ThYwBusiness thYwBusinessParam = new ThYwBusiness();
		thYwBusinessParam.setDpStatus(DpStatus.NORMAL.getCode());
		thYwBusinessParam.setBusiId(busiId);
		if(StringUtils.isBlank(busiType)) { //单元测试可以检测
			ExceptionHelper.thow("9999", "业务类型不能为空");
		}
		thYwBusinessParam.setBusiType(busiType);
		ThYwBusiness thYwBusiness = thYwBusinessMapper.selectRefined(thYwBusinessParam);
		boolean insert = false;
		if(null == thYwBusiness) {
			insert = true;
			thYwBusiness = new ThYwBusiness();
			thYwBusiness.initInsertData(currentUserId, currentUserName);
		} else {
			thYwBusiness.initUpdateData(currentUserId, currentUserName);
		}
		// 自有业务	zy  鑫合汇	xhh
		thYwBusiness.setBusiType(busiType);
		thYwBusiness.setThirdId(xeApplyNo);
		thYwBusiness.setBusiId(busiId);
		if(null != ywBusinessDetail) {
			thYwBusiness.setCusId(ywBusinessDetail.getCustomerId());
			thYwBusiness.setDealerId(ywBusinessDetail.getDealerId());
			thYwBusiness.setDealerName(ywBusinessDetail.getDealerName());
			thYwBusiness.setSalersmanIdThen(ywBusinessDetail.getSalesmanId());
			thYwBusiness.setSalersmanCodeThen(ywBusinessDetail.getSalesmanCode());
			thYwBusiness.setSalersmanNameThen(ywBusinessDetail.getSalesmanName());
			thYwBusiness.setBankName(ywBusinessDetail.getLoanBankDisplay());
			thYwBusiness.setBankCode(ywBusinessDetail.getLoanBank());
		}
		thYwBusiness.setSalersmanProvinceThen(salermanProvince);
		//客户相关信息
		thYwBusiness.setCustomerName(applyLoanRequest.getName());
		thYwBusiness.setCustomerNo(applyLoanRequest.getCardNo());
		thYwBusiness.setCustomerTel(applyLoanRequest.getMobile1());
		thYwBusiness.setSpouseName(applyLoanRequest.getSpouseName());
		thYwBusiness.setSpouseNo(applyLoanRequest.getSpouseCardNo());
		thYwBusiness.setSpouseTel(applyLoanRequest.getSpouseMobile());
		
		if(null != clientType) {
			thYwBusiness.setClientType(clientType.getCode());
		}
		if(null != thirdBusiStatus) {
			thYwBusiness.setThirdBusiStatus(thirdBusiStatus.getCode());
		}
		if(null != thirdCompensatoryStatus) {
			thYwBusiness.setThirdCompensatoryStatus(thirdCompensatoryStatus.getCode());
		}
		
//		thYwBusiness.setFundsToAccountTime(fundsToAccountTime);
//		thYwBusiness.setActualRaiseFee(actualRaiseFee);
//		thYwBusiness.setBorrowerStartDate(borrowerStartDate);
//		thYwBusiness.setBorrowerRepayDate(borrowerRepayDate);
//		thYwBusiness.setDateRepayment(dateRepayment);
		
//		thYwBusiness.setAddedStatus1(addedStatus1);
//		thYwBusiness.setAddedStatus2(addedStatus2);
//		thYwBusiness.setAddedStatus3(addedStatus3);
		
		if(insert) {
			thYwBusinessMapper.insert(thYwBusiness);
		} else {
			thYwBusinessMapper.updateByPrimaryKeySelective(thYwBusiness);
		}
		return thYwBusiness;
	}
	
	
	@Override
	public void saveUpdateAndDelete(ThYwBusiness thYwBusiness, OperateType operType) {
		switch (operType) {
		case add:
			thYwBusiness.initInsertData(thYwBusiness.getModifier(), thYwBusiness.getModifierName());
			thYwBusinessMapper.insert(thYwBusiness);
			break;
		case edit:
			thYwBusiness.initUpdateData(thYwBusiness.getModifier(), thYwBusiness.getModifierName());
			// 影响的记录数
			int updateCount = thYwBusinessMapper.updateByPrimaryKeySelective(thYwBusiness);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			thYwBusiness.initDeleteData(thYwBusiness.getModifier(), thYwBusiness.getModifierName());
			// 影响的记录数
			int deleteCount = thYwBusinessMapper.updateByPrimaryKeySelective(thYwBusiness);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<ThYwBusiness> selectByExt(ThYwBusinessExt thYwBusinessExt) {
		return thYwBusinessMapper.selectByExt(thYwBusinessExt);
	}
	
	@Override
	public int countByExt(ThYwBusinessExt thYwBusinessExt){
		return thYwBusinessMapper.countByExt(thYwBusinessExt);
	}
	
	@Override
	public ThYwBusiness selectById(String id) {
		return thYwBusinessMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			thYwBusinessMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(ThYwBusiness thYwBusiness) {
		return thYwBusinessMapper.countRefined(thYwBusiness);
	}

	@Override
	public ThYwBusiness selectRefined(ThYwBusiness thYwBusiness) {
		return thYwBusinessMapper.selectRefined(thYwBusiness);
	}
	
	@Override
	public int deletedRefined(ThYwBusiness thYwBusiness) {
		return thYwBusinessMapper.deletedRefined(thYwBusiness);
	}

	@Override
	public boolean repeat(ThYwBusiness thYwBusiness) {
		int count = countRefined(thYwBusiness);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(thYwBusiness.getId())) {
			return true;
		}
		ThYwBusiness dbItem = selectRefined(thYwBusiness);
		if(null == dbItem) {
			return false;
		}
		return !thYwBusiness.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return thYwBusinessMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<ThYwBusiness> thYwBusinesss) {
		return thYwBusinessMapper.insertBatch(thYwBusinesss);
	}
}
